/********************************************/
/*	Copyright (C) 2018 Gong Li Bin			*/
/*	Project:	GmRm2000-1.0.0				*/
/*	Author:		gong_libin					*/
/*	Date:		2018_07_01					*/
/*	File:		GmTcp.go					*/
/********************************************/

package proto

import (
	"fmt"
	"encoding/binary"
)
const (
	TH_FIN		= 0x01
	TH_SYN		= 0x02
	TH_RST		= 0x04
	TH_PUSH		= 0x08
	TH_ACK		= 0x10
	TH_URG		= 0x20
)

type GmTcp struct {
	Sport	uint16		/* source port */
	Dport	uint16		/* destination port */
	Seq		uint32		/* sequence number */
	Ack		uint32		/* acknowledgement number */
	ThOff	uint8		/* data offset */
	Flags	uint8
	Win		uint16		/* window */
	Sum		uint16		/* checksum */
	Urp		uint16		/* urgent pointer */
}

func NewGmTcp(buf []byte) *GmTcp {
	return &GmTcp{
		Sport: uint16(binary.BigEndian.Uint16(buf[0:2])),
		Dport: uint16(binary.BigEndian.Uint16(buf[2:4])),
		Seq: uint32(binary.BigEndian.Uint32(buf[4:8])),
		Ack: uint32(binary.BigEndian.Uint32(buf[8:12])),
		ThOff: uint8(buf[12]) >> 4,
		Flags: uint8(buf[13]),
		Win: uint16(binary.BigEndian.Uint16(buf[14:16])),
		Sum: uint16(binary.BigEndian.Uint16(buf[16:18])),
	}
}

func (v GmTcp)GmPrint() {
	fmt.Printf("Sport: %d, ", v.Sport)
	fmt.Printf("Dport: %d, ", v.Dport)
	fmt.Printf("Seq: %d, ", v.Seq)
	fmt.Printf("Ack: %d, ", v.Ack)
	fmt.Printf("ThOff: %d\n", v.ThOff)
	fmt.Printf("Urg: %d, Ack: %d, ", (v.Flags & TH_URG) >> 5, (v.Flags & TH_ACK) >> 4)
	fmt.Printf("Psh: %d, Rst: %d, ", (v.Flags & TH_PUSH) >> 3, (v.Flags & TH_RST) >> 2)
	fmt.Printf("Psh: %d, Rst: %d, ", (v.Flags & TH_PUSH) >> 3, (v.Flags & TH_RST) >> 2)
	fmt.Printf("Syn: %d, Fin: %d\n", (v.Flags & TH_SYN) >> 1, v.Flags & TH_FIN)
	fmt.Printf("Win: %d, ", v.Win)
	fmt.Printf("Sum: 0x%04x\n", v.Sum)
}